package todolist.activities;

import java.util.List;

import todolist.activities.R;
import todolist.helpers.userDataSource;
import todolist.structures.User;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * Shows the login screen
 * If new account button is clicked, changes to create new account screen 
 *
 */
public class LoginActivity extends Activity implements OnClickListener {
    
	userDataSource datasource;
	List<User> values;
	TodoList context;
	private boolean invalidLogin;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        context = ((TodoList) getApplicationContext());
        
        TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
        Button loginButton = (Button) findViewById(R.id.btnLogin);
        
        registerScreen.setOnClickListener(this);
        loginButton.setOnClickListener(this);
        
        datasource = new userDataSource(this);
		datasource.open();

		
	
		// Use the SimpleCursorAdapter to show the
		// elements in a ListView
		//ArrayAdapter<User> adapter = new ArrayAdapter<User>(this,
			//	android.R.layout.simple_list_item_1, values);
		//setListAdapter(adapter);
	}

	// Will be called via the onClick attribute
	// of the buttons in main.xml
	public void onClick(View v) {
		User user = null;
		switch (v.getId()) {

        /*
         *Still needs to support hash functionality 
         *Executed when the login button is clicked
         */
		case R.id.btnLogin:
			EditText name = (EditText) findViewById(R.id.log_username);
			EditText password = (EditText) findViewById(R.id.log_password);
			
			//Checking for null data in the username or password fields
			if(checkForNull(name, password)) {
				Toast.makeText(LoginActivity.this, "Please enter Username and/or Password",
					Toast.LENGTH_LONG).show();
			}
			
			
			//Username/password fields are NOT blank
			else {

				//retrieve users from database
				values = datasource.getAllUsers();
				
				//search for username in database
				for(int i=0; i<values.size(); i++){

					//check to see if names match
					if(values.get(i).getName().compareTo(name.getText().toString())==0) {
						user=values.get(i);
					}
				}
				
				//If we did not find a matching username in the database (user does not exist)
				if(user==null){
					setInvalidLogin(true);
					Toast.makeText(LoginActivity.this, "User Authentication Failed",
						Toast.LENGTH_LONG).show();
				}
				
				//We found the username and the password we have on file matches
				else if (user.getPassword().compareTo(password.getText().toString())==0) {
					setInvalidLogin(false);
					Toast.makeText(LoginActivity.this, "User Authenticated Succesfully",
						Toast.LENGTH_LONG).show();
					
					//Flag this as the currentuser
					context.setCurrentUserId((int)user.getId());
					
					//Proceed to the tasklist
					Intent i = new Intent(getApplicationContext(), ListScreenActivity.class);
					startActivity(i);
				}
				
				//The password was incorrect
				else {
					Toast.makeText(LoginActivity.this, "User Authentication Failed",
						Toast.LENGTH_LONG).show();
				}
			}
			
			break;
		
		//Executed when the register button is clicked
		case R.id.link_to_register:
			startRegisterActivity();
			break;
		}
		//adapter.notifyDataSetChanged();
	}

	@Override
	protected void onResume() {
		datasource.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		datasource.close();
		super.onPause();
	}
	
	public boolean checkForNull(EditText name, EditText password) {
		if(name.getText().toString().compareTo("")==0 || password.getText().toString().compareTo("")==0){
			return true;
		}
		else {
			return false;
		}
	}
	
	public boolean isInvalidLogin() {
		return invalidLogin;
	}

	public void setInvalidLogin(boolean invalidLogin) {
		this.invalidLogin = invalidLogin;
	}
	
	public void startRegisterActivity() {
		Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
		startActivity(i);

	}
}